package com.ykh.mybbsdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mybbsdemo.dao.mapper.ArticleMapper;
import com.ykh.mybbsdemo.dao.mapper.FocusedMapper;
import com.ykh.mybbsdemo.dao.mapper.MessageMapper;
import com.ykh.mybbsdemo.dao.mapper.SysUserMapper;
import com.ykh.mybbsdemo.dao.pojo.po.Article;
import com.ykh.mybbsdemo.dao.pojo.po.Focused;
import com.ykh.mybbsdemo.dao.pojo.po.Message;
import com.ykh.mybbsdemo.dao.pojo.po.SysUser;
import com.ykh.mybbsdemo.service.MessageServie;
import com.ykh.mybbsdemo.dao.pojo.vo.ArticleMessageVo;
import com.ykh.mybbsdemo.dao.pojo.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 202;
 * 3-06-08
 * Time: 14:00
 */
@Service
public class MessageServiceImpl implements MessageServie {
    @Autowired
    MessageMapper messageMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    FocusedMapper focusedMapper;

    /**
     * 获取自己的消息列表
     * @param sysUser
     * @return
     */
    @Override
    public Result getMessageList(SysUser sysUser) {
        //查出该用户作为接收方的 信息
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getAcceptUserId, sysUser.getId());
        List<Message> messages = messageMapper.selectList(queryWrapper);

        List<Long> articleIdList = new ArrayList<>();
        List<Long> authorIdList = new ArrayList<>();

        if( messages.size()==0){
            return Result.fail(405,"暂无消息");
        }
        for(Message message: messages){
            articleIdList.add(message.getArticleId());
        }
        //根据文章id 查出文章信息
        LambdaQueryWrapper<Article> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(Article::getId, articleIdList);
        List<Article> articles = articleMapper.selectList(queryWrapper1);

        //转化为vo
        List<ArticleMessageVo> articleMessageVos = copyList(articles);

        //设置查出作者名称
        LambdaQueryWrapper<SysUser> queryWrapper2 = new LambdaQueryWrapper<>();
        for (Article article: articles) {
            articleIdList.add(article.getAuthorId());
        }
        queryWrapper2.in(SysUser::getId, articleIdList);
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper2);
        for(int i = 0;i<articles.size();i++){
            if(i<sysUsers.size()){
                articleMessageVos.get(i).setAuthorName( sysUsers.get(i).getNickname());
            } else {
                articleMessageVos.get(i).setAuthorName( sysUsers.get(sysUsers.size()-1).getNickname());
            }

            articleMessageVos.get(i).setIsRead( messages.get(i).getIsRead());
        }
        return Result.success( articleMessageVos);

    }
//    /**
//     * 获取自己的消息列表(视图查询)
//     * @param sysUser
//     * @return
//     */
//    @Override
//    public Result getMessageList(SysUser sysUser) {
//        List<ArticleMessageVo>articleMessageVos =  messageMapper.getMessageList(sysUser.getId());
//        for(ArticleMessageVo articleMessageVo: articleMessageVos){
//            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(SysUser::getId, articleMessageVo.getAuthorId());
//            SysUser sysUser1 = sysUserMapper.selectOne(queryWrapper);
//            articleMessageVo.setAuthorName(sysUser1.getNickname());
//        }
//
//        return Result.success( articleMessageVos);
//    }
//
//
    /**
     * 推送消息给关注他的用户
     * @param
     * @return
     */
    @Override
    public Result addMessage(Long articleId, Long authorId) {
        List<Long> acceptIdList = new ArrayList<>();
        //先查关注表, 获取他的用户id列表
        LambdaQueryWrapper<Focused> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Focused::getFocusedId, authorId);
        List<Focused> focuseds = focusedMapper.selectList(queryWrapper);
        for (Focused focused:focuseds) {
            acceptIdList.add(focused.getLaunchId());
        }

        //构造消息, 往消息表插入
        for(int i=0; i< acceptIdList.size();i++){
            Message message = new Message();
            message.setArticleId( articleId );
            message.setAcceptUserId(acceptIdList.get(i));
            message.setLaunchUserId(authorId);
            message.setIsRead(0);
            messageMapper.insert(message);
        }
        return Result.success("消息推送成功");
    }

    private List<ArticleMessageVo> copyList(List<Article> articles) {
        List<ArticleMessageVo> articleMessageVos = new ArrayList<>();
        for (Article article: articles) {
            articleMessageVos.add( copy(article));
        }
        return articleMessageVos;
    }

    private ArticleMessageVo copy(Article article) {
        ArticleMessageVo articleMessageVo = new ArticleMessageVo();
        BeanUtils.copyProperties(article, articleMessageVo);
        articleMessageVo.setUserId(article.getId());
        articleMessageVo.setArticleTitle(article.getTitle());
        articleMessageVo.setArticleId(article.getId());
        return articleMessageVo;
    }
}
